<?php
class weixin{
    public function __construct()
    {
        echo "微信支付";
    }
}
class PayPal{
    public function __construct()
    {
        echo "PayPal支付";
    }
}

class zhifubao{
    public function __construct()
    {
        echo "支付宝支付";
    }
}

class 支付模块{
    protected $power = [];

    public function __construct()
    {
        $this->power[] = new weixin();
        $this->power[] = new PayPal();
        $this->power[] = new zhifubao();
    }
}
$superman = new 支付模块();
print_r($superman);
echo "<hr>";

class 简单工厂{
    public function __construct($string)
    {
        $power = null;
        switch ($string){
            case "weixin":
                printf("实例化了：%s", $string);
                $power =  new weixin();
                break;
            case "PayPal":
                printf("实例化了：%s", $string);
                $power =  new PayPal();
                break;
            case "zhifubao":
                printf("实例化了：%s", $string);
                $power =  new zhifubao();
                break;
//                ......
            default:
                printf("没有任何：%s", '空');
        }

        return $power;
    }
}

class 支付模块2{
    protected $power = [];

    public function __construct($string)
    {
        $this->power[] = new 简单工厂($string);
    }
}

$superman = new 支付模块2('zhifubao');
print_r($superman);
echo "<hr>";

//////////////////////////////////////////////////////////////////////////////////
///
interface zhifuInterface{
    public function createOrder();
    public function cancelOrder();
}

class weixinFactory implements zhifuInterface {
    public function createOrder(){
        echo "您的订单号是 WX111111";
    }
    public function cancelOrder(){

    }
}
class paypalFactory implements zhifuInterface {
    public function __construct()
    {
    }

    public function createOrder(){
        echo "您的订单号是 PP222222";
    }
    public function cancelOrder(){

    }
}
class zhifubaoFactory implements zhifuInterface {
    public function createOrder(){
        echo "您的订单号是 ZFB333333";
    }
    public function cancelOrder(){

    }
}



class 支付模块3{
    protected $power = null;
    public function __construct(zhifuInterface $zhifu)
    {
        $this->power = $zhifu;
        $this->power->createOrder();
    }
    public function index($a,$b){
        echo $a.$b;
    }
}

$superman = new 支付模块3(new zhifubaoFactory());
print_r($superman);
echo "<hr>";
$use = call_user_func_array(['支付模块3','index'], ['aaa','bbb']);
$use = call_user_func_array([$superman,'index'], ['ccc','ddd']);
echo "<hr>";


class factorya{
    public $a;
    public function __construct($string)
    {
        $reflector = new ReflectionClass($string);
        $DetermineIfInstantiable = $reflector->isInstantiable();    //判断是否可实例化
        $constructor = $reflector->getConstructor();
        print_r($constructor->getParameters());
        if (is_null($constructor) || empty($constructor->getParameters())){
            $instantion = new $string;  //有参数时会报错
            print_r($instantion);
            $this->a = $instantion;
        }else{
            $instantionWithParam = $reflector->newInstanceArgs(['suibian','2222']);
            print_r($instantionWithParam);
            $this->a = $instantionWithParam;
        }
    }
}

//$aa = new factorya('paypalFactory');
//print_r($aa->a);
//参考：https://liuwangshu.blog.csdn.net/article/details/52326959
//https://segmentfault.com/a/1190000016480587
//https://www.jc2182.com/php/php-reflectionclassnewinstanceargs-ref.html
class 支付模块4{
    protected $power = null;
    public function __construct($string)
    {
        $this->power = (new factorya($string))->a;
        $this->power->createOrder();
    }
    public function index($a,$b){
        echo $a.$b;
    }
}
$superman = new 支付模块4('paypalFactory');
print_r($superman);
